// ========================================================
// Des：
// Autor：Mr.H 
// CreateTime：2024/04/15 13:17:39
// 版 本：v 1.0
// ========================================================

using System;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

public class Lesson7 : MonoBehaviour
{
    public Button btn;
    public Button btn1;
    public Button btn2;
    public Button btn3;
    public InputField input;
    // Start is called before the first frame update
    void Start()
    {
        btn.onClick.AddListener(() =>
        {
            // if(input.text != "")
            //     NetMgr.Instance.Send(input.text);
            PlayerMsg ms = new()
            {
                playerID = 1111,
                playerData = new PlayerData()
            };
            ms.playerData.name = "唐老狮客户端发送的信息";
            ms.playerData.atk = 22;
            ms.playerData.lev = 10;
            NetMgr.Instance.Send(ms);
        });

        btn1.onClick.AddListener(async() =>
        {
            //分包
            PlayerMsg ms3 = new()
            {
                playerID = 1003,
                playerData = new PlayerData()
            };
            ms3.playerData.name = "唐老狮3客户端发送的信息";
            ms3.playerData.atk = 3;
            ms3.playerData.lev = 33;
            byte[] bytes = ms3.Writing();
            //分包
            byte[] byte1 = new byte[10];
            byte[] byte2 = new byte[bytes.Length - 10];
            //第一个包
            Array.Copy(bytes, 0, byte1, 0, 10);
            //第二个包
            Array.Copy(bytes, 10, byte2, 0, bytes.Length - 10);
            NetMgr.Instance.SendTest(byte1);
            await Task.Delay(1000);
            NetMgr.Instance.SendTest(byte2);


        });
        btn2.onClick.AddListener(() =>
        {
            //粘包
            PlayerMsg ms1 = new()
            {
                playerID = 1001,
                playerData = new PlayerData()
            };
            ms1.playerData.name = "唐老狮1客户端发送的信息";
            ms1.playerData.atk = 1;
            ms1.playerData.lev = 11;

            PlayerMsg ms2 = new()
            {
                playerID = 1002,
                playerData = new PlayerData()
            };
            ms2.playerData.name = "唐老狮2客户端发送的信息";
            ms2.playerData.atk = 2;
            ms2.playerData.lev = 22;

            byte[] bytes = new byte[ms1.GetBytesNum() + ms2.GetBytesNum()];
            ms1.Writing().CopyTo(bytes, 0);
            ms2.Writing().CopyTo(bytes, ms1.GetBytesNum());
            NetMgr.Instance.SendTest(bytes);
        });
        btn3.onClick.AddListener(() =>
        {
            //分+粘包
        });
    }

    // Update is called once per frame
    void Update()
    {

    }
}

